﻿
/*
	添加一条公共信息给当前所有有效的用户
*/

CREATE PROCEDURE [dbo].[Sys_MessageManager_NewMessageToAllUser]
	@messageContent  nvarchar(500) ,
	@creatorUserID int
AS

	DECLARE @OutputTbl TABLE (ID INT)
	declare @messageID int

	set @messageID=-1;

	begin try
	   Begin  Tran 

		INSERT INTO sys_UserMessage([MessageContent], [CreatorUserID], [CreateDt],[IsDeleted])
		OUTPUT INSERTED.MessageID INTO @OutputTbl(ID)
		VALUES (@messageContent, @creatorUserID, GETDATE(),0)

		select @messageID=id from @OutputTbl

		if @messageID>0
		begin

			insert into sys_MessageUserMapping (UserID,MessageID,[IsRead],[ReadDt])
			select UserID,@messageID,0,null from [sys_LoginUsers] where UserID<>@creatorUserID and IsStop=0 and IsDeleted=0

			commit tran
		end
		else
		begin
			rollback tran
		end

	end try
	begin catch
		rollback tran
	end catch

RETURN @messageID
